package com.huawei123.hasee.cniao5store.mvp.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.adapter.section.TopContactsSection;
import com.huawei123.hasee.cniao5store.adapter.top.TopTopWrapper;
import com.huawei123.hasee.cniao5store.base.BaseMvpFragment;
import com.huawei123.hasee.cniao5store.bean.AppBean;
import com.huawei123.hasee.cniao5store.bean.TopBean;
import com.huawei123.hasee.cniao5store.mvp.presenter.impl.TopPresenterImpl;
import com.huawei123.hasee.cniao5store.mvp.view.view.TopFragmentView;
import com.huawei123.hasee.cniao5store.utils.UIUtils;
import com.huawei123.hasee.cniao5store.view.LoadingPager;
import com.huawei123.hasee.cniao5store.view.widget.ViewUpSearch;
import com.zhxu.recyclerview.section.SectionRVAdapter;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：Cniao5Store
 * 类描述：排行界面
 * 创建人：Administrator
 * 创建时间：2017/10/25 0025 下午 4:59
 * 修改人：Administrator
 * 修改时间：2017/10/25 0025 下午 4:59
 * 修改备注：
 */

public class TopFragment extends BaseMvpFragment<TopPresenterImpl> implements TopFragmentView {
    @BindView(R.id.mRV_top)
    RecyclerView mRV_top;
    @BindView(R.id.mVUS_search)
    ViewUpSearch mVUS_search;

    @Inject
    public TopPresenterImpl topPresenterImpl;
    private TopBean topBean;

    @Override
    protected void loading() {
        topPresenterImpl.getTopData(myActivity);
        //                new Thread(new Runnable() {
        //                    @Override
        //                    public void run() {
        //                        SystemClock.sleep(2000);
        //                        setState(LoadingPager.LoadResult.empty);
        //                    }
        //                }).start();
    }

    boolean isUnfold = true;

    @Override
    protected View createSuccessView() {
        View top_fragment = UIUtils.inflate(R.layout.fragment_top);
        ButterKnife.bind(this, top_fragment);

        SectionRVAdapter sectionAdapter = new SectionRVAdapter(getContext());

        Map<String, List<AppBean>> appBeanMap = topBean.getAppBeanMap();
        Set<String> strings = appBeanMap.keySet();

        for (String name : strings) {
            List<AppBean> appBeanList = appBeanMap.get(name);
            TopContactsSection section = new TopContactsSection(getContext(), name, appBeanList);

            sectionAdapter.addSection(section);
        }

        TopTopWrapper topTopWrapper = new TopTopWrapper(getContext(), sectionAdapter);
        topTopWrapper.addDataAll(topBean.getTopTopBeanList());

        mRV_top.setLayoutManager(new LinearLayoutManager(getContext()));
        mRV_top.setAdapter(topTopWrapper);
        mRV_top.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) mRV_top.getLayoutManager();
                int visibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                if (visibleItemPosition == 0) {
                    if (dy > 0 && isUnfold) {
                        isUnfold = !isUnfold;
                        mVUS_search.updateShow(isUnfold);
                    } else if (dy < 0 && !isUnfold) {
                        isUnfold = !isUnfold;
                        mVUS_search.updateShow(isUnfold);
                    }
                }
            }
        });
        return top_fragment;
    }


    @Override
    public void requestDataSuccess(TopBean topBean) {
        this.topBean = topBean;
        setState(LoadingPager.LoadResult.success);
    }

    @Override
    public void requestDataError(String errmsg) {
        setState(LoadingPager.LoadResult.error);
    }

    @Override
    public void requestDataEmpty() {
        setState(LoadingPager.LoadResult.empty);
    }

    @Override
    protected TopPresenterImpl initPresenterInjector() {
        //使用父类BaseMvpFragment实例化好的topComponent对象注入本Fragment对象
        fragmentComponent.inject(this);
        //返回注入的Presenter
        return topPresenterImpl;
    }
}
